package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.ItemModelDao;
import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.model.EquipmentType;
import mil.af.amc.adpetracker.model.ItemModel;
import mil.af.amc.adpetracker.model.Manufacturer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Hibernate implementation of the ItemModelDao
 *
 * @author TSgt Jason Ferguson
 */

@SuppressWarnings({"UnnecessaryLocalVariable", "unchecked"})
@Repository("itemModelDao")
public class HibernateItemModelDao extends HibernateGenericDao<ItemModel> implements ItemModelDao {

    Log log = LogFactory.getLog(HibernateItemModelDao.class);

    public HibernateItemModelDao() {
        super(ItemModel.class);
    }

    public List<ItemModel> getAllByManufacturer(Manufacturer manu) {

        Criteria imCriteria = sessionFactory.getCurrentSession().createCriteria(ItemModel.class);
        imCriteria.addOrder(Order.asc("name"));

        Criteria manufacturerCriteria = imCriteria.createCriteria("manufacturer");
        manufacturerCriteria.add(Restrictions.ilike("name", manu.getName()));

//        DetachedCriteria crit = DetachedCriteria.forClass(ItemModel.class);
//        DetachedCriteria manufacturerCrit = crit.createCriteria("manufacturer");
//        manufacturerCrit.add(Restrictions.ilike("name", manu.getName()));
//
//        crit.addOrder(Order.asc("name"));

        List<ItemModel> resultList = null;
        try {
            resultList = imCriteria.list();
        } catch (DataAccessException e) {
            log.debug(e);
        }

        return resultList;

    }

    public List<ItemModel> getItemModelsByEquipmentType(final EquipmentType equipmentType) {

        Criteria eiCriteria = sessionFactory.getCurrentSession().createCriteria(EquipmentType.class);
        Criteria imCriteria = eiCriteria.createCriteria("itemModel");
        Criteria etCriteria = imCriteria.createCriteria("type");

        etCriteria.add(Restrictions.ilike("name", equipmentType.getName()));

        List<EquipmentItem> eiResults = eiCriteria.list();
        Set<ItemModel> results = new HashSet<ItemModel>();
        for (EquipmentItem ei: eiResults) {
            results.add(ei.getItemModel());
        }

        return new ArrayList<ItemModel>(results);
//        DetachedCriteria crit = DetachedCriteria.forClass(EquipmentItem.class);
//        // narrow down to the model
//        DetachedCriteria etCrit = crit.createCriteria("itemModel");
//
//        // narrow down to the type
//        DetachedCriteria subEtCrit = etCrit.createCriteria("type");
//
//        subEtCrit.add(Restrictions.ilike("name", equipmentType.getName()));

//        List<EquipmentItem> equipmentItemResults = getHibernateTemplate().findByCriteria(crit);
//        List<ItemModel> results = new ArrayList<ItemModel>();
//
//        for (Iterator<EquipmentItem> it = equipmentItemResults.iterator(); it.hasNext();) {
//            EquipmentItem item = it.next();
//            if (!results.contains(item.getItemModel())) {
//                results.add(item.getItemModel());
//            }
//        }
//        return results;
    }

    public List<ItemModel> getAllByItemTypeAndManufacturer(EquipmentType type, Manufacturer manu) {

        Criteria imCriteria = sessionFactory.getCurrentSession().createCriteria(ItemModel.class);
        Criteria etCriteria = imCriteria.createCriteria("type");

        etCriteria.add(Restrictions.eq("id", type.getId()));
        //DetachedCriteria crit = DetachedCriteria.forClass(ItemModel.class);
        //DetachedCriteria typeCrit = crit.createCriteria("type");
        //typeCrit.add(Restrictions.eq("id", type.getId()));

        Criteria manufacturerCriteria = imCriteria.createCriteria("manufacturer");
        //DetachedCriteria manufacturerCrit = crit.createCriteria("manufacturer");
        manufacturerCriteria.add(Restrictions.eq("id", manu.getId()));

        List<ItemModel> results = imCriteria.list();

        return results;
        //return (List<ItemModel>) getHibernateTemplate().findByCriteria(crit);
    }
}
